// 定义Messages接口以匹配后端的数据结构
interface Messages {
  // 根据实际的Messages类结构添加属性
  content: string
  isUser: boolean,
 role: 'user' | 'system'; // 更语义化且类型安全
  
}

import { baseRequestClient, requestClient } from '#/api/request';
import { useAccessStore } from '@vben/stores';



export async function sendModelApi(messages: Messages[]) {
  const response = await requestClient.post('/modelApi', messages,{
        responseType: 'stream'
    });
  return response
}
      const accessStore = useAccessStore();
export async function* streamModelApi(messages: Messages[]) {
  const res = await fetch('/api/modelApi', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      "satoken":accessStore.accessToken
    },
    body: JSON.stringify(messages),
  });

  if (!res.ok) {
    throw new Error('请求失败');
  }

  const reader = res.body?.getReader();
  const decoder = new TextDecoder();
  let result = '';

  while (true) {
    const { done, value } = await reader!.read();
    if (done) break;

    const chunk = decoder.decode(value, { stream: true });
    result += chunk;

    yield chunk; // 每次返回一个 chunk，用于更新 UI
  }
}
